<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.input_fields.php 336 2013-10-01 19:21:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 336 $
* 	zuletzt geaendert von:	$Author$
* 	Datum:					$Date: 2013-10-01 19:21:11 +0000 (Tue, 01 Oct 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_FS_INC.'inc.get_age.php');

class input_fields {
	public $file_name;
	public $field_name;
	public $check = false;
	public $parameter;
	public $required = false;
	public $error = 'false';
	public $customers_status;
	public $customers_vat_id_status;
	public $post;
	
	public $post_vars = array();
	
	public function __construct($file, $post = '') {
		$this->file_name = $file;
		if(!empty($post))
			$this->post = $post;
	}
	
	public function __destruct() {}
	
	public function getField($field_name, $value = '', $parameter = '') {
		if(isset($this->post['action']) && ($this->post['action'] == 'process'))
			$field = $this->checkData($field_name, $value, $parameter);
		else
			$field = $this->getData($field_name, $value, $parameter);
										
		return $field;
	}
	
	public function getData($field_name, $value = '', $parameter = '') {
		global $db;
		
		$this->field_name = $field_name;
		$this->required = '';
		
		$get_data = $db->db_query("SELECT 
										show_".$this->file_name.", 
										require_".$this->file_name.",
										field_type, 
										min_length, 
										max_length,
										error_desc
									FROM 
										".TABLE_INPUT_FIELDS." 
									WHERE 
										field_name = '".$field_name."' ");
		
		if($get_data->fields['show_'.$this->file_name] == 1) {				
			if($get_data->fields['require_'.$this->file_name] == '1')
				$this->required = ' <span class="inputRequirement">*</span>';
			switch($get_data->fields['field_type']) {
				case 'text':
					$field = $this->getFieldText($value, $parameter);
					break;
				
				case 'password':
					$field = $this->getFieldPassword($value, $parameter);
					break;
					
				case 'radio':
					$field = $this->getFieldRadio($value, $parameter);
					break;
				
				case 'checkbox':
					$field = $this->getFieldCheckbox('', $parameter, $value);
					break;
					
				case 'dropdown':
					$field = $this->getFieldDropdown($value, $parameter);
					break;
			}
			return $field;
		}
	}
	
	public function checkData($field_name, $value = '', $parameter = '') {
		global $db;
		
		$this->field_name = $field_name;
		$this->required = '';
		$this->field_error = '';
		
		$get_data = $db->db_query("SELECT 
										show_".$this->file_name.", 
										require_".$this->file_name.",
										field_type, 
										min_length, 
										max_length,
										error_desc
									FROM 
										".TABLE_INPUT_FIELDS." 
									WHERE 
										field_name = '".$field_name."' ");
		
		if($get_data->fields['show_'.$this->file_name] == 1) {
			if(isset($this->post[$this->field_name])) {
				switch($this->field_name) {
					case 'dob':
							if(!preg_match('|[0-9]{2}\.[0-9]{2}\.[0-9]{4}|', $this->post['dob'])) {
								if($get_data->fields['require_'.$this->file_name] == 1) {
									$this->error = 'true';
									$this->field_error = 'true';
									$this->required = ' <span class="inputRequirement">'.constant($get_data->fields['error_desc']).'</span>';
								} 
								else if($this->post['dob'] != '') {
									$this->required = ' <span class="inputRequirement">'.constant($get_data->fields['error_desc']).'</span>';
									$this->field_error = 'true';
								}
							}
							else {
                $dob = explode('.', $this->post['dob']);
                $dob = mktime(0, 0, 0, intval($dob[1]), intval($dob[0]), intval($dob[2]));
                if (get_age($dob) < 18) {
									$this->error = 'true';
									$this->field_error = 'true';
									$this->required = ' <span class="inputRequirement">Mindestalter von 18 Jahren erforderlich</span>';
                }
              }
						break;
						
					case 'vat':
						require_once(DIR_WS_CLASSES.'class.vat_validation.php');
						$vatID = new vat_validation($this->post['vat'], '', '', $this->post['country']);
	
						$this->customers_status = $vatID->vat_info['status'];
						$this->customers_vat_id_status = $vatID->vat_info['vat_id_status'];
						
						if($vatID->vat_info['error']) {
							$this->error = 'true';
							$this->field_error = 'true';
							$this->required = ' <span class="inputRequirement">'.ENTRY_VAT_ERROR.'</span>';
						}	
						break;
						
					case 'email':
						if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $this->post['email'])) {
							$this->error = 'true';
							$this->field_error = 'true';
							$this->required = ' <span class="inputRequirement">'.ENTRY_EMAIL_ADDRESS_CHECK_ERROR.'</span>';
						}
						break;
						
					case 'email_confirmation':
						if($this->post['email'] != $this->post['email_confirmation']) {
							$this->error = 'true';
							$this->field_error = 'true';
							$this->required = ' <span class="inputRequirement">'.ENTRY_EMAIL_ADDRESS_CONFIRMATION_ERROR.'</span>';
						}
						break;
						
					case 'confirmation':
						if($this->post['password'] != $this->post['confirmation']) {
							$this->error = 'true';
							$this->field_error = 'true';
							$this->required = ' <span class="inputRequirement">'.constant($get_data->fields['error_desc']).'</span>';
						}
						break;
						
						
				  case 'postcode':
						if(!preg_match("/^[0-9]{4,5}$/", $this->post['postcode'])) {
							$this->error = 'true';
							$this->field_error = 'true';
							$this->required = ' <span class="inputRequirement">'.constant($get_data->fields['error_desc']).'</span>';
						}
						else {
						  if (isset($this->post['country']) && $this->post['country'] == '81') { // DE
  						  $val_url = 'http://www.postdirekt.de/plzserver/PlzAjaxServlet?nocache='.time().'&adv_plz='.$this->post['postcode'].'&finda=adv&lang=de_DE';
  						  $msg = json_decode(file_get_contents($val_url), true);
  						  if ($msg != null && $msg['count'] == 0) {
                	$this->error = 'true';
    							$this->field_error = 'true';
    							$this->required = ' <span class="inputRequirement">'.ENTRY_POST_CODE_VALIDATION_ERROR.'</span>';
                }
              }
            }
          
            break;
              	
					case 'gender':
					case 'newsletter':
						break;
						
					default:
						if(!empty($get_data->fields['min_length']) && $get_data->fields['min_length'] != 0 && (strlen($this->post[$this->field_name]) < $get_data->fields['min_length']) && !empty($this->post[$this->field_name]) && $get_data->fields['require_'.$this->file_name] == 1) {
							$this->required = ' <span class="inputRequirement">'.sprintf(constant($get_data->fields['error_desc']), $get_data->fields['min_length']).'</span>';
							$this->error = 'true';
							$this->field_error = 'true';
							
						} elseif(!empty($get_data->fields['max_length']) && $get_data->fields['max_length'] != 0 && ($get_data->fields['require_'.$this->file_name] == 1) && (strlen($this->post[$this->field_name]) > $get_data->fields['max_length'])) {
							
							$this->required = ' <span class="inputRequirement">'.constant($get_data->fields['error_desc']).'</span>';
							$this->error = 'true';
							$this->field_error = 'true';
							
						} elseif($get_data->fields['require_'.$this->file_name] == 1 && empty($this->post[$this->field_name])) {
							$this->error = 'true';
							$this->field_error = 'true';
							$this->required = ' <span class="inputRequirement">'.constant($get_data->fields['error_desc']).'</span>';
						}
							
						break;
				}
				
			} elseif($get_data->fields['require_'.$this->file_name] == 1 && empty($this->post[$this->field_name])) {
				$this->error = 'true';
				$this->field_error = 'true';
			}
			
			switch($get_data->fields['field_type']) {
				case 'text' :
					$field = $this->getFieldText($this->post[$this->field_name], $parameter);
					break;
					
				case 'password' :
					$field = $this->getFieldPassword($this->post[$this->field_name], $parameter);
					break;
				
				case 'dropdown' :
					$field = $this->getFieldDropdown($this->post[$this->field_name], $parameter);
					break;
				
				case 'checkbox':
					$field = $this->getFieldCheckbox($value, $parameter);
					break;
				
				case 'radio' :
					$field = $this->getFieldRadio($value, $parameter);
					break;
			}
			$this->post_vars[$this->field_name] = $this->post[$this->field_name];
			return $field;
		}
	}
	
	public function getFieldRadio($value = '', $parameter = '') {
		$post = false;
		if(!empty($parameter))
			$post = true;
		elseif(($value == $this->post[$this->field_name]) || (!isset($this->post['action']) && $value == 'm'))
			$post = true;

		return '<input type="radio" name="'.$this->field_name.'" '.$parameter.' value="'.$value.'"'.($post ? ' checked=""' : '').' /> '.$this->required;
	}
	
	public function getFieldText($post = '', $parameter = '', $value = '') {
		if($this->field_error == 'true' && !empty($parameter))
			$parameter = str_replace('class="', 'class="error_border ', $parameter);
			
		elseif($this->field_error == 'true' && empty($parameter))
			$parameter = ' class="error_border"';
			
		else
			$parameter = $parameter;
		
		if($this->field_name == 'dob' && $post == '0000-00-00 00:00:00')
			$post = '';
		
		return '<input type="text" name="'.$this->field_name.'" '.$parameter.' value="'.$post.'" /> '.$this->required;
	}
	
	public function getFieldCheckbox($post = '', $parameter = '', $value = '') {
		if(isset($_POST[$this->post[$this->field_name]]))
			$post = ' checked=""';
		else
			$post = '';
			
		if($this->field_error == 'true')
			$field = '<span class="error_border">';
			
		$field .= '<input type="checkbox" name="'.$this->field_name.'" '.$parameter.' value="'.$value.'"'.$post.' /> '.$this->required;
		
		if($this->field_error == 'true')
			$field .= '</span>';
		
		return $field;
	}
	
	public function getFieldPassword($post = '', $parameter = '', $value = '') {
		if($this->field_error == 'true' && !empty($parameter))
			$parameter = str_replace('class="', 'class="error_border ', $parameter);
			
		elseif($this->field_error == 'true' && empty($parameter))
			$parameter = ' class="error_border"';
			
		else
			$parameter = $parameter;
		return '<input type="password" name="'.$this->field_name.'" '.$parameter.' value="" /> '.$this->required;
	}
	
	public function getFieldDropdown($value = '', $parameter = '') {
		global $db;

		switch($this->field_name) {
			case 'state':
				$zones_array = array();
				$zones = $db->db_query("SELECT 
											zone_id,
											zone_name 
										FROM 
											".TABLE_ZONES." 
										WHERE 
											zone_country_id = '".(isset($this->post['country']) ? (int)$this->post['country'] : STORE_COUNTRY)."' 
										ORDER BY 
											zone_name");
				while(!$zones->EOF) {
					$zones_array[] = array ('id' => $zones->fields['zone_id'], 'text' => $zones->fields['zone_name']);
					$zones->MoveNext();
				}
				return draw_pull_down_menu($this->field_name, $zones_array, $value, $parameter).$this->required;
				break;
				
			case 'country':
				if(!empty($value))
					$selected = $value;
				else
					$selected = STORE_COUNTRY;
					
				$countries_array = array();
				$countries = $db->db_query("SELECT countries_id, countries_name FROM ".TABLE_COUNTRIES." WHERE status = '1' ORDER BY countries_name");
				while(!$countries->EOF) {
					$countries_array[] = array('id' => $countries->fields['countries_id'], 'text' => $countries->fields['countries_name']);
					$countries->MoveNext();
				}
				return draw_pull_down_menu($this->field_name, $countries_array, $selected, $parameter).$this->required;
				break;
		}
	}
}